﻿#include<iostream>
using namespace std;

enum color
{
	red,
	black
};

template<class K,class V>
struct	RBTreeNode
{
	pair<K, V> _kv;
	RBTreeNode* _left;
	RBTreeNode* _right;
	RBTreeNode* _parent;
	color _color;

	RBTreeNode(const pair<K.V>& kv)
		:_kv(kv)
		,_left(nullptr)
		,_right(nullptr)
		,_parent(nullptr)
	{

	}
};

template<class K,class V>
class RBTree
{
	typedef RBTreeNode<K, V> Node;
public:
	bool Insert(pair<K, V>& kv)
	{
		//1.先找插入点
		if (_root == nullptr)
		{
			_root = new Node(kv);
			_root->_color = black;
			return true;
		}

		Node* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{
			if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(kv);
		cur->_color = red;

		//2.找到父亲节点
		if (parent->_kv.first < cur->_kv.first)
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}
		cur->_parent = parent;

		//3.开始插入(如果要插入的节点的父亲是黑就不用处理了)
		while (parent&& parent->_color = red)
		{
			Node* grandfather = parent->_parent;
			//  g
			// p u
			//<1>parent在左边
			if (parent == grandfather->_left)
			{
				Node* uncle = grandfather->_right;
				//(1)uncle存在且为红色
				if (uncle && uncle->_color == red)
				{
					parent->_color = uncle->_color = black;
					grandfather->_color = red;
					// u存在且为红 ->变⾊再继续往上处理 
					cur = grandfather;
					parent = cur->_parent;
				}
				//(2)uncle不存在或为黑色
				else
				{
					//  g
                    // p u
                    //c
                    //单旋 
					if (cur = parent->_left)
					{
						RotateR(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						// g
						// p u
						// c
						//双旋 
						RotateL(parent);
						RotateR(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
				}
				break;
			}
			else
			{
				//<2>parent在右边
				//  g
				// u p
				Node* uncle = grandfather->_left;
				// 叔叔存在且为红，-》变⾊即可 
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;
					// 继续往上处理 
						cur = grandfather;
					parent = cur->_parent;
				}
				else // 叔叔不存在，或者存在且为⿊ 
				{
					// 情况⼆：叔叔不存在或者存在且为⿊ 
					// 旋转+变⾊ 
					//   g
					//  u  p
					//       c
					if (cur == parent->_right)
					{
						RotateL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						//  g
						// u p
						//  c
						RotateR(parent);
						RotateL(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}
			}
		}
		_root->_col = BLACK;
		return turn;
	}

		Node * Find(const K & key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < key)
			{
				cur = cur->_right;
			}
			else if (cur->_kv.first > key)
			{
				cur = cur->_left;
			}
			else
			{
				return cur;
			}
		}
		return nullptr;
	}

private:
	Node* _root = nullptr;

	void _InOrder(Node* root)
	{
		if (root == nullptr)
			return;

		_InOrder(root->_left);
		cout << root->_kv.first << " ";
		//cout << root->_kv.first << ":" << root->_kv.second << endl;
		_InOrder(root->_right);
	}


	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		if (subLR)
			subLR->_parent = parent;

		Node* parentParent = parent->_parent;

		subL->_right = parent;
		parent->_parent = subL;

		if (parent == _root)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (parentParent->_left == parent)
			{
				parentParent->_left = subL;
			}
			else
			{
				parentParent->_right = subL;
			}

			subL->_parent = parentParent;
		}
	}

	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		Node* parentParent = parent->_parent;

		subR->_left = parent;
		parent->_parent = subR;

		if (parent == _root)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (parentParent->_left == parent)
			{
				parentParent->_left = subR;
			}
			else
			{
				parentParent->_right = subR;
			}

			subR->_parent = parentParent;
		}
	}
};

